﻿<div class="wrap-container">
    <form id="cate" class="layui-form" style="width: 90%;padding-top: 20px;height: 400px ;">
        <input type="hidden" name="CategoryId" />
        <div class="layui-form-item">
            <label class="layui-form-label">上级：</label>
            <div class="layui-input-block">
                <select name="Parentid" lay-verify="required">
                    <option>123</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称：</label>
            <div class="layui-input-block">
                <input type="text" name="Cname" required lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
@section script{
<script>
			//Demo
			layui.use(['form'], function() {
				var form = layui.form();
				var option= "<option value=''>请选择类型</option>";
				$.post("/Home/parenttable", function (datas) {
				 		 for(var i=0;i<datas.length;i++){
               				 option+="<option value='"+datas[i].id+"'>"+datas[i].pname+"</option>";//拼接option中的内容
                			$("select[name='Parentid']").html(option);//将option的拼接内容加入select中，注意选择器是select的ID
          			 }
				 		 form.render('select');
				 })
				form.render();
				//监听提交
				form.on('submit(formDemo)', function(data) {
					//layer.msg(JSON.stringify(data));
					//parent.layer.msg('Hi, man');
					AddCategory();
					return false;
				});
			});
			function AddCategory(){
				$.ajax({
					type:"post",
					url: "/Home/addcategory",
					data: $("#cate").serialize(),
					dataType:'Json',
					async:true,
					success:function(data){
						parent.layer.msg(data);
					}
				});
			}
</script>

}